{% extends "../layouts/admin.html" %} {% block content %}
<header class="header navbar bg-white shadow">
  <div class="btn-group tool-button">
    <a class="btn btn-primary navbar-btn" href="/weixin/mass/index?id={{wxid}}" data-pjax><i
        class="ti-angle-left"></i> 返回</a>
  </div>
</header>

<div class="content-wrap">
  <div class="wrapper" style="min-height:500px;">
    <section class="panel panel-form">
      <form id="addForm" role="form" class="form-horizontal parsley-form" data-parsley-validate
            action="/weixin/mass/send" method="post">
        <input id="wxid" name="wxid" value="{{wxid}}" type="hidden">
        <div class="row mb10">
          <div class="col-lg-12">
            <div class="form-group">
              <label for="content" class="col-sm-2 control-label">群发名称</label>

              <div class="col-sm-8">
                <input id="name" name="name" type="text" class="form-control">

              </div>
            </div>
            <div class="form-group">
              <label for="content" class="col-sm-2 control-label">群发类型</label>

              <div class="col-sm-8">
                <div class="radio">
                  <label>
                    <input type="radio" name="type" value="text" checked>文本消息
                  </label>
                </div>
                <div class="radio">
                  <label>
                    <input type="radio" name="type" value="news">图文消息
                  </label>
                </div>
              </div>
            </div>
            <div class="form-group" id="div_text">
              <label for="content" class="col-sm-2 control-label">文本内容</label>

              <div class="col-sm-8">
                <textarea id="content" name="content" cols="20" rows="5" class="form-control"></textarea>

              </div>
            </div>
            <div class="form-group" id="div_news" style="display: none">
              <label for="content" class="col-sm-2 control-label">图文内容</label>

              <div class="col-sm-8">
                <div class="btn-group tool-button">
                  <a class="btn btn-primary navbar-btn" id="sel"> 选择内容</a>
                  <a class="btn btn-danger navbar-btn" id="del">清除内容</a>

                </div>
                <div class="dd" id="nestable3">
                  <ol class="dd-list">
                  </ol>
                </div>
              </div>
            </div>
            <div class="form-group">
              <label for="content" class="col-sm-2 control-label">发送范围</label>

              <div class="col-sm-8">
                <div class="radio"><label>
                    <input type="radio" name="scope" value="all" checked>全部会员</label></div>
                <div class="radio">
                  <label> <input type="radio" name="scope" value="some">手动填写</label></div>
              </div>
            </div>
            <div class="form-group" id="div_scope" style="display: none">
              <label for="content" class="col-sm-2 control-label"></label>

              <div class="col-sm-8">
                <textarea id="openids" name="openids" cols="20" rows="5" class="form-control"></textarea>
                <span style="color: red">openid必须2个以上</span><span>并且使用英文,符号分割</span>
              </div>
            </div>
          </div>
        </div>
        <div class="col-lg-3"></div>
        <div class="col-lg-6">
          <div class="form-group text-center">
            <label></label>

            <div>
              <button class="btn btn-primary btn-block btn-lg btn-parsley" data-loading-text="正在提交..." id="submit">提 交
              </button>
            </div>
          </div>
        </div>
      </form>
    </section>

  </div>
</div>
<div id="dialogSelect" class="modal fade bs-modal-sm" tabindex="-3" role="dialog" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">

    </div>
  </div>
</div>
<script language="JavaScript">
  $(document).ready(function () {
    $("input:radio[name=type]").on("click",function(){
      $("#content").val("");
      if($(this).val()=='text'){
        $("#div_text").show();
        $("#div_news").hide();
        $("#nestable3 .dd-list").html("");
      }else {
        $("#div_text").hide();
        $("#div_news").show();
      }
    });
    $("input:radio[name=scope]").on("click",function(){
      if($(this).val()=='all'){
        $("#div_scope").hide();
        $("#openids").val("");
      }else {
        $("#div_scope").show();
      }
    });
    $("#dialogSelect").on("hidden.bs.modal", function () {
      $(this).removeData("bs.modal");
    });
    $("#sel").on("click", function () {
      var msgtype = $('input:radio[name=msgtype]:checked').val();
      $("#dialogSelect").modal({
        remote: "/weixin/mass/select?id={{wxid}}"
      });
    });
    $("#del").on("click", function () {
      $("#nestable3 .dd-list").html("");
    });
    $("#submit").on("click", function () {
      var data = $("#nestable3").nestable('serialize');
      var ids = [];
      if (data) {
        $.each(data, function (i, n) {
          ids.push(n.id);
        });
      }
      if($("#name").val()==''){
        Toast.error("请填写群发名称");
        $("#name").focus();
        return false;
      }
      if ($("input[name=type]:checked").val()=='news'&&(ids.length < 1 || ids.indexOf("nestable3") > -1)) {
        Toast.error("请选择图文");
        return false;
      } else if ($("input[name=type]:checked").val()=='news'&&ids.length > 10) {
        Toast.error("图文消息不能大于10条");
        return false;
      } else if($("input[name=type]:checked").val()=='text'&& $("#content").val()==''){
        Toast.error("请填写文本内容");
        $("#content").focus();
        return false;
      }
      if($("input[name=scope]:checked").val()=='some'&& $("#openids").val()==''){
        Toast.error("请填写openid");
        $("#openids").focus();
        return false;
      }
      if($("input[name=type]:checked").val()=='news'){
        $("#content").val(ids.toString());
      }
      $('#addForm').ajaxForm({
        dataType: 'json',
        beforeSubmit: function (arr, form, options) {

          form.find("button:submit").button("loading");
        },
        success: function (data, statusText, xhr, form) {
          if (data.errno == 0) {
            Toast.success(data.data);

          } else {
            Toast.error(data.errmsg);
          }
          form.find("button:submit").button("reset");
        }
      });
    });

  });
</script>
{% endblock %}